package com.mlearning.demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mlearning.demo.mapper.TreeMapper;
import com.mlearning.demo.mapper.UserMapper;
import com.mlearning.demo.pojo.neo4j.Book;
import com.mlearning.demo.pojo.neo4j.Mastered;
import com.mlearning.demo.pojo.neo4j.Tree;
import com.mlearning.demo.pojo.neo4j.User;
import com.mlearning.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TreeMapper treeMapper;

    @Override
    public boolean creat(String name, String email, String password) {
        int old_user = userMapper.select_email(email);
        if (old_user == 0) {
            userMapper.add(name, password, email, new Date().getTime(),"这个人很懒，什么都没有写");
            System.out.println("插入用户数据成功");
            return true;
        } else {
            return false;
        }
    }

    @Override
    public User select(String email, String password) {

        return userMapper.select_one(email, password);
    }

    @Override
    public boolean find_email(String email) {
        int old_user = userMapper.select_email(email);
        if(old_user == 0) {
            return false;
        } else {
            return true;
        }
    }

//    @Override
//    public JSONObject select() {
//        List<User> all_user = userMapper.select_all();
//        if (all_user.size() == 0) {
//            return null;
//        }
//
//        JSONObject userJson = new JSONObject();
////        System.out.println(all_user.size());
//        List<String> names = new ArrayList<>();
//        List<String> passwords = new ArrayList<>();
//        for (int i = 0; i < all_user.size(); i++) {
//            names.add(all_user.get(i).getName());
//            passwords.add(all_user.get(i).getPassword());
//        }
//        userJson.put("name", names);
//        userJson.put("password", passwords);
//
//        return userJson;
//    }

    @Override
    public void delete(String name) {
        userMapper.delete(name);
    }

    @Override
    public ArrayList<Tree> getMyTrees(int userid){
        JSONArray rawTrees =JSONArray.parseArray(treeMapper.getMyTrees(userid).toString());
//        System.out.println(rawTrees);
        ArrayList<String> treeMastered=treeMapper.getTreeMastered(userid);
        ArrayList<Tree> trees=new ArrayList<>();
        for(Object rawTree:rawTrees){
            Tree tree=new Tree();
            ArrayList<String> knows=new ArrayList<>();
            ArrayList<Boolean> flags =new ArrayList<>() ;
            for(Object i:(JSONArray)rawTree){
                knows.add(i.toString());
                flags.add(treeMastered.contains(i.toString()));
            }
            tree.setKnows(knows);
            tree.setFlags(flags);
            trees.add(tree);
        }
//        System.out.println(trees);
        return trees;
    }
    @Override
    public String updateMyTrees(int userid){
        JSONArray rawTrees =JSONArray.parseArray(treeMapper.getMyTrees(userid).toString());
//        System.out.println(rawTrees);
        ArrayList<String> treeMastered=treeMapper.getTreeMastered(userid);
        ArrayList<Tree> trees=new ArrayList<>();
        for(Object rawTree:rawTrees){
            Tree tree=new Tree();
            ArrayList<String> knows=new ArrayList<>();
            ArrayList<Boolean> flags =new ArrayList<>() ;
            for(Object i:(JSONArray)rawTree){
                knows.add(i.toString());
                flags.add(treeMastered.contains(i.toString()));
            }
            tree.setKnows(knows);
            tree.setFlags(flags);
            trees.add(tree);
        }
        return JSON.toJSONString(trees);
    }
    @Override
    public ArrayList<String> getRecKnows(int userid){
        return userMapper.getRecKnows(userid);
    }
    @Override
    public ArrayList<Mastered>getMastered(int userid){
        return userMapper.getMastered(userid);
    }
    @Override
    public String updateMastered(int userid){
        return JSON.toJSONString(userMapper.getMastered(userid));
    }
    @Override
    public int getMasteredQuan(int userid){
        return userMapper.getMasteredQuan(userid);
    }
    @Override
    public ArrayList<Book>getMyBooks(int userid){
        return userMapper.getMyBooks(userid);
    }
    @Override
    public String updateMyBooks(int userid){
        return JSON.toJSONString(userMapper.getMyBooks(userid));
    }
    @Override
    public int getMyBooksQuan(int userid){
        return userMapper.getMyBooksQuan(userid);
    }
    @Override
    public boolean isMastered(int userid,String knowName){
//        System.out.println(userMapper.isMastered(userid,knowName));
        return userMapper.isMastered(userid,knowName)>0;
    }
    @Override
    public int addMyBook(int bookId,int userId,long timestamp){
        if(getMyBooksQuan(userId)>=20){
            return -1;
        }
        return userMapper.addMyBook(bookId, userId,timestamp);
    }
    @Override
    public int delMyBook(int bookId,int userId){
        return userMapper.delMyBook(bookId, userId);
    }
    @Override
    public int edit(int userid,int sort,String content){
        switch(sort){
            case 1: return userMapper.editName(userid,content);
            case 2: return userMapper.editSign(userid,content);
            case 3: return userMapper.editEmail(userid,content);
            case 4: return userMapper.editPassword(userid,content);
            default: return -1;
        }
    }
}
